import path from './config.js'

const getApi = (url = '', params = {}) => {
	if(!params.channelType){
		params.channelType = '1'
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: url, //仅为示例，并非真实接口地址。
			data: params,
			header: {
				'Content-Type':'application/json;charset=UTF-8',
				'Content-Type':'application/x-www-form-urlencoded'
			},
			method: 'GET',
			dataType: "json",  /* 如设为json，会对返回的数据做一次 JSON.parse */
			responseType: "text",
			success: (res) => {
				resolve(res)
			},
			fail: (res) => {
				resolve(null)
			}
		})
	})
}

const postApi = (url = '', params = {}) => {
	
	if(!params.channelType){
		params.channelType = '5'
	}
	
	// if(!params.memberId){
	// 	params.memberId = '4f0abd1af84d4168ba27ac00b63fcb2e'
	// }
	
	console.log("接口:" + url + "请求参数------->",params);
	uni.showLoading({
		title:'加载中',
		mask:true
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: url, //仅为示例，并非真实接口地址。
			data: params,
			header: {
				'Content-Type':'application/json'
			},
			method: 'POST',
			dataType: "json",  /* 如设为json，会对返回的数据做一次 JSON.parse */
			responseType: "text",
			success: (res) => {
				uni.hideLoading()
				resolve(res)		
			},
			fail: (res) => {
				uni.hideLoading()
				resolve(null)

			}
		})
	})
}

const getOppenId = (url = '', params = {}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url, //仅为示例，并非真实接口地址。
			data: params,
			header: {
				'Content-Type':'application/json;charset=UTF-8',
				'Content-Type':'application/x-www-form-urlencoded'
			},
			method: 'GET',
			dataType: "json",  /* 如设为json，会对返回的数据做一次 JSON.parse */
			responseType: "text",
			success: (res) => {
				resolve(res)
			},
			fail: (res) => {
				resolve(res)
			}
		})
	})
}

export { getApi, postApi, getOppenId }
